package com.billy.ohos.swipe.demo.consumer;


import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.SmartSwipeRefresh;
import com.billy.ohos.swipe.SmartSwipeWrapper;
import com.billy.ohos.swipe.SwipeConsumer;
import com.billy.ohos.swipe.consumer.ShuttersConsumer;
import com.billy.ohos.swipe.consumer.SlidingConsumer;
import com.billy.ohos.swipe.demo.BaseRecyclerViewAbility;
import com.billy.ohos.swipe.demo.ResourceTable;
import com.billy.ohos.swipe.listener.SimpleSwipeListener;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

/**
 * demo:
 * @author billy.qi
 */
public class ShuttersConsumerAbility extends BaseRecyclerViewAbility {

    @Override
    public int getTitleResId() {
        return ResourceTable.String_demo_ui_ShuttersConsumer;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        //enable recyclerView top & bottom swipe
         smartSwipeRefresh = SmartSwipeRefresh.behindMode(findComponentById(ResourceTable.Id_container_refresh), false)
                .setDataLoader(dataLoader);
        smartSwipeRefresh.getSwipeConsumer()
                .as(SlidingConsumer.class)
                .setEdgeAffinity(true);
    }

    @Override
    protected ViewHolder createRecyclerViewHolder(Component view) {
        itemConsumer = SmartSwipe.wrapView(view)
                .addConsumer(new ShuttersConsumer())
                .enableHorizontal();
        return new ViewHolder(itemConsumer.getWrapper(), itemConsumer);
    }

    class ViewHolder extends BaseRecyclerViewAbility.ViewHolder {

        ViewHolder(SmartSwipeWrapper wrapper, SwipeConsumer consumer) {
            super(wrapper);
            consumer.addListener(new SimpleSwipeListener(){
                @Override
                public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
                    adapter.removeItem(getPosition());                }
            });
        }

    }
}
